Imports CapaDeDatos
Public Class nFacturaCompra
    Public Factura As CapaDeDatos.dFacturaCompra = New CapaDeDatos.dFacturaCompra
    Public Proveedor As CapaDeDatos.dProveedor = New CapaDeDatos.dProveedor
    Public Condicion As CapaDeDatos.dCondicion = New CapaDeDatos.dCondicion

    Public ChequesPropios As CapaDeNegocios.nChequesPropios = New CapaDeNegocios.nChequesPropios
    Public FacOtras As CapaDeNegocios.nFacOtras = New CapaDeNegocios.nFacOtras
    Public FacCuenta As CapaDeNegocios.nFacCuenta = New CapaDeNegocios.nFacCuenta
    Public FacCheque As CapaDeNegocios.nFacCheque = New CapaDeNegocios.nFacCheque
    Public ChequeTercero As CapaDeNegocios.nChequesTerceros = New CapaDeNegocios.nChequesTerceros


    Public Function InsertarFactura(ByVal CodigoProv As Integer, ByVal Numero As String, _
    ByVal Estado As String, ByVal Total As Double, ByVal Iva As Double, ByVal IdCondCompra As String, ByVal Observacion As String) As Integer
        Return Factura.Insertar(CodigoProv, Numero, Estado, Total, Iva, IdCondCompra, Observacion)
    End Function
    Public Function Existe(ByVal Numero As String, ByVal CodigoProv As Integer) As Boolean
        Return Factura.Existe(Numero, CodigoProv)
    End Function
    Public Function ListarFacturas(ByVal CodigoFactura As Integer, ByVal CodigoProv As Integer, ByVal Numero As String, _
                                    ByVal Estado As String, ByVal Total As Integer, ByVal Iva As Integer, _
                                    ByVal IdCondCompra As String, ByVal Observacion As String) As DataSet
        Return Factura.BuscarFiltrado(CodigoFactura, CodigoProv, Numero, Estado, Total, Iva, IdCondCompra, Observacion)
    End Function
    Public Function Modificar(ByVal CodigoFactura As String, ByVal CodigoProv As String, ByVal Numero As String, ByVal Estado As String, ByVal Total As String, ByVal Iva As String, ByVal IdCondCompra As String, ByVal Observacion As String) As Boolean
        Return Factura.Modificar(CodigoFactura, CodigoProv, Numero, Estado, Total, Iva, IdCondCompra, Observacion)
    End Function
    Public Function ListarFacturasCompletas(ByVal CodigoFactura As Integer, ByVal CodigoProv As Integer, ByVal Numero As String, _
                                    ByVal Estado As String, ByVal Total As Integer, ByVal Iva As Integer, _
                                    ByVal IdCondCompra As String, ByVal Observacion As String) As DataSet
        Dim dtsPrincipal As DataSet
        dtsPrincipal = Factura.BuscarFiltrado(CodigoFactura, CodigoProv, Numero, Estado, Total, Iva, IdCondCompra, Observacion)
        dtsPrincipal.Tables(0).Columns.Add("RazonSocial")
        dtsPrincipal.Tables(0).Columns.Add("DescripcionCond")
        Dim i As Integer
        For i = 0 To dtsPrincipal.Tables(0).Rows.Count - 1
            Try
                dtsPrincipal.Tables(0).Rows(i)("RazonSocial") = Proveedor.BuscarFiltrado(dtsPrincipal.Tables(0).Rows(i)("CodigoProv").ToString(), "", "", "", "", "", "", "", "").Tables(0).Rows(0)("RazonSocial").ToString()
            Catch ex As Exception
                dtsPrincipal.Tables(0).Rows(i)("RazonSocial") = "Error, Razon Social no Encontrada"
            End Try
            Try
                dtsPrincipal.Tables(0).Rows(i)("DescripcionCond") = Condicion.BuscarFiltrado(dtsPrincipal.Tables(0).Rows(i)("IdCondCompra").ToString(), "", 0, "Compra").Tables(0).Rows(0)("Descripcion").ToString()
            Catch ex As Exception
                dtsPrincipal.Tables(0).Rows(i)("DescripcionCond") = "Contado"
            End Try
        Next
        Return dtsPrincipal
    End Function
    Public Function EliminarFactura(ByVal CodigoFactura As Integer) As Boolean
        Return Factura.Eliminar(CodigoFactura)
    End Function
    Public Function GrabarImputaciones(ByVal Dtsimputaciones As DataSet, ByVal CodigoFactura As Integer, ByVal Fecha As String, ByVal Proveedor As String) As Boolean
        Dim okas As Boolean = True
        Dim i As Integer
        If Not (Dtsimputaciones Is Nothing) Then
            For i = 0 To Dtsimputaciones.Tables(0).Rows.Count - 1
                If Dtsimputaciones.Tables(0).Rows(i)("Banco").ToString() <> "" And _
                Dtsimputaciones.Tables(0).Rows(i)("Cuenta").ToString() <> "" And _
                Dtsimputaciones.Tables(0).Rows(i)("Chequera").ToString() <> "" And _
                Dtsimputaciones.Tables(0).Rows(i)("Cheque").ToString() <> "" Then

                    ChequesPropios.InsertarChequePropio(Dtsimputaciones.Tables(0).Rows(i)("Cheque").ToString(), _
                              Dtsimputaciones.Tables(0).Rows(i)("Chequera").ToString(), _
                              Proveedor, Dtsimputaciones.Tables(0).Rows(i)("Total").ToString(), Fecha, "", Proveedor, Proveedor, "")
                    FacCheque.InsertaFacCheque(CodigoFactura, Dtsimputaciones.Tables(0).Rows(i)("Cheque").ToString(), _
                              Dtsimputaciones.Tables(0).Rows(i)("Total").ToString(), Fecha)
                End If
                If Dtsimputaciones.Tables(0).Rows(i)("Banco").ToString() <> "" And _
                Dtsimputaciones.Tables(0).Rows(i)("Cuenta").ToString() <> "" And _
                Dtsimputaciones.Tables(0).Rows(i)("Chequera").ToString() = "" Then
                    FacCuenta.InsertaCuenta(Dtsimputaciones.Tables(0).Rows(i)("Cuenta").ToString(), CodigoFactura, Dtsimputaciones.Tables(0).Rows(i)("Total").ToString(), Fecha)
                End If
                If Dtsimputaciones.Tables(0).Rows(i)("Nombre").ToString() <> "" And _
                Dtsimputaciones.Tables(0).Rows(i)("Banco").ToString() = "" And _
                Dtsimputaciones.Tables(0).Rows(i)("Idcheque").ToString() = "" Then
                    FacOtras.InsertarRegistro(CodigoFactura, 2, Dtsimputaciones.Tables(0).Rows(i)("Total").ToString(), Fecha)
                End If
                If Dtsimputaciones.Tables(0).Rows(0)("IdCheque").ToString() <> "" Then
                    ChequeTercero.ModificarCheque(Dtsimputaciones.Tables(0).Rows(0)("IdCheque").ToString(), "", "", "", "", "", "", "", "Endosado")

                End If
            Next
        End If
    End Function
End Class
